<?php
namespace app\service;
use think\Db;
use app\service\wechatService;
class wechatCarService{
    public function arrive(){
       
        $data['appid'] = 'wx352362f198d9c56b';//公众账号ID
        $data['mch_id'] = '1484539292';//商户号
        $data['sub_appid'] = 'wx6e64e3c7372be385';//小程序id  
		$data['sub_mch_id']="1500219082";//  1511534341
        $data['nonce_str'] = (new wechatService())->createNoncestr();//随机字符串
        //$data['out_trade_no'] ="20190823110523123456";//商户订单号
        $data['version'] = "2.0";//总金额
		$data['trade_scene']="PARKING";//浙AAAAA1
		$data['sign_type']="HMAC-SHA256";//HMAC-SHA256
		$data['scene_info']='{"scene_info":{"start_time":"20190823110523","plate_number":"浙D61S21","free_time":"1200","car_type":"大型车","parking_name":"西溪永乐城停车场"}}';
        
        $wxkey = 'asdfaASDFASDFASFasfsadf6546A84SA';    
        $data['sign'] = (new wechatService())->MakeSign($data,$wxkey);//签名
		echo "<pre>";
		var_dump($data);
		$yzl=(new wechatService())->ToXml($data);
		Log::write($yzl,"yangyzl");
        $xml = (new wechatService())->ToXml($data);
				
        $url = 'https://api.mch.weixin.qq.com/vehicle/partnerpay/notification';
        $payInfo = (new wechatService())->post_curl_data($url,$xml);
        $payInfo =json_encode(simplexml_load_string($payInfo, 'SimpleXMLElement', LIBXML_NOCDATA));
		var_dump($payInfo);
        $payInfo = json_decode( $payInfo, true);
        
        var_dump($payInfo);
	}
    
	//用户状态查询
	public function querystate($car_number,$parkinginfo){
		$webinfo=$this->webinfo();
        $data['appid'] = $webinfo["appid"];//公众账号ID
		$data['sub_appid'] = $webinfo["sub_appid"];//小程序id 
        $data['mch_id'] = $webinfo["mch_id"];//商户号
		$data['sub_mch_id']=$parkinginfo["sub_mch_id"];//
        $data['nonce_str'] = (new wechatService())->createNoncestr();//随机字符串
		$data['sign_type']="HMAC-SHA256";
		$data['trade_scene']="PARKING";
        $data['jump_scene'] ="";//商户跳转的业务场景，不传默认是小程序，也支持APP和公众号H5跳转APP:通过APP跳转 H5:通过公众号H5跳转
        $data['version'] = "2.0";//版本号
		$data['plate_number']=$car_number;//浙D61S21
        $wxkey =  $webinfo["wxkey"];    
        $data['sign'] = (new wechatService())->MakeSign($data,$wxkey);//签名
        
        $xml = (new wechatService())->ToXml($data);
        
        $url = 'https://api.mch.weixin.qq.com/vehicle/partnerpay/querystate';
        $result = (new wechatService())->post_curl_data($url,$xml);
        
        $result = json_decode(json_encode(simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $result;
	}
	
	//申请扣款
	public function payapply($orderinfo,$out_time,$pay_money,$parkinginfo){
		$webinfo=$this->webinfo();
        $data['appid'] = $webinfo["appid"];//公众账号ID
		$data['sub_appid'] = $webinfo["sub_appid"];//小程序id 
        $data['mch_id'] = $webinfo["mch_id"];//商户号
		$data['sub_mch_id']=$parkinginfo["sub_mch_id"];
        $data['nonce_str'] = (new wechatService())->createNoncestr();//随机字符串
		$data['sign_type']="HMAC-SHA256";
		
        $data['body'] ="停车代扣";//商户跳转的业务场景，不传默认是小程序，也支持APP和公众号H5跳转APP:通过APP跳转 H5:通过公众号H5跳转
        $data['out_trade_no'] = $orderinfo["out_order_no"];//订单号
		$data['total_fee']=$pay_money*100;
		$data['spbill_create_ip'] = $_SERVER["REMOTE_ADDR"];//终端IP
		$data['notify_url'] = 'https://www.fqsc.zfbzhsq.com/wxpro/order/wxnotice';//通知url
		$data['trade_type']='PAP';
		$data['version']='2.0';
		$data['trade_scene']="PARKING";
		$charging_time=$out_time-$orderinfo['in_time'];
		$data['scene_info']='{"scene_info":{"start_time":"'.date('YmdHis',$orderinfo['in_time']).'","	
end_time":"'.date('YmdHis',$out_time).'","plate_number":"'.$orderinfo["car_number"].'","free_time":"0","charging_time":"'.$charging_time.'","parking_name":"'.$orderinfo["parking_name"].'"}}';
        
        $wxkey =$webinfo["wxkey"];    
        $data['sign'] = (new wechatService())->MakeSign($data,$wxkey);//签名
        
        $xml = (new wechatService())->ToXml($data);
        
        $url = 'https://api.mch.weixin.qq.com/vehicle/partnerpay/payapply';
        $result = (new wechatService())->post_curl_data($url,$xml);
        
        $result = json_decode(json_encode(simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $result;
	}
	
	public function webinfo(){
		return Db::table("webset")->where("id","1")->find();
	}
}